<template>
  <div class="task-container">
    <el-card class="box-card">
      <div class="search">
          <span>年级</span>
          <el-select 
            v-model="query.gradeLevel" 
            style="width:300px;margin-left:5px;"
            clearable
            @clear="getTaskList">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <el-button type="primary" class="searchBtn" @click="getTaskList">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="addTask">创建任务</el-button>
      </div>
      <div class="table">
        <el-table
            width="100%"
            :data="taskList"
            stripe border>
            <el-table-column
              prop="id"
              label="id"
              width="60px">
            </el-table-column>
            <el-table-column
              prop="title"
              label="标题">
            </el-table-column>
            <el-table-column label="年级" width="120px">
              <template slot-scope="scope">
                <span v-if="scope.row.gradeLevel=== 1">一年级</span>
                <span v-if="scope.row.gradeLevel=== 2">二年级</span>
                <span v-if="scope.row.gradeLevel=== 3">三年级</span>
                <span v-if="scope.row.gradeLevel=== 4">四年级</span>
                <span v-if="scope.row.gradeLevel=== 5">五年级</span>
                <span v-if="scope.row.gradeLevel=== 6">六年级</span>
                <span v-if="scope.row.gradeLevel=== 7">初一</span>
                <span v-if="scope.row.gradeLevel=== 8">初二</span>
                <span v-if="scope.row.gradeLevel=== 9">初三</span>
                <span v-if="scope.row.gradeLevel=== 10">高一</span>
                <span v-if="scope.row.gradeLevel=== 11">高二</span>
                <span v-if="scope.row.gradeLevel=== 12">高三</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="createUserName"
              label="发送人">
            </el-table-column>
            <el-table-column label="创建时间" width="180px">
              <template slot-scope="scope">
                  {{scope.row.createTime | dateFormat}}
              </template>
            </el-table-column>
            <el-table-column label="操作" >
                <template slot-scope="scope">
                  <div class="btns">
                    <el-button class="btn" @click="editTask(scope.row.id)">编辑</el-button>
                    <el-button class="btn" @click="deleteTask(scope.row.id)">删除</el-button>
                  </div>
                </template>
            </el-table-column>
          </el-table>
          <!-- 分页区域 -->
          <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                class="pagination"
                :current-page="query.pageIndex"
                :page-sizes="[1, 2, 5, 10]"
                :page-size="query.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
          </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data(){
    return {
      //查询参数
      query:{
        pageIndex: 1,
        pageSize: 5,
        gradeLevel: null
      },
      taskList:[],//任务列表
      total: 0,//总数
      //年级选择
      options: [{
          value: '1',
          label: '一年级'
        }, {
          value: '2',
          label: '二年级'
        }, {
          value: '3',
          label: '三年级'
        }, {
          value: '4',
          label: '四年级'
        }, {
          value: '5',
          label: '五年级'
        }, {
          value: '6',
          label: '六年级'
        }, {
          value: '7',
          label: '初一'
        }, {
          value: '8',
          label: '初二'
        }, {
          value: '9',
          label: '初三'
        }, {
          value: '10',
          label: '高一'
        }, {
          value: '11',
          label: '高二'
        }, {
          value: '12',
          label: '高三'
        }],
    }
  },
  created(){
    this.getTaskList();
  },
  methods:{
    async getTaskList(){
      const resp = await this.getRequest("/api/admin/task/page",{
        pageIndex: this.query.pageIndex,
        pageSize: this.query.pageSize,
        gradeLevel: this.query.gradeLevel
      });
      if(resp){
        this.taskList = resp.data;
        this.total = resp.total;
      }
    },
     //监听pagesize改变的事件
    handleSizeChange(newSize) {
        //console.log(newSize);
        this.query.pageSize = newSize
        this.getTaskList();
    },
    //监听页码值改变的事件
    handleCurrentChange(newPage) {
        //console.log(newPage);
        this.query.pageIndex = newPage
        this.getTaskList();
    },
    //创建任务
    addTask(){
      this.$router.replace("/admin/task/create");
    },
    //删除任务
    deleteTask(id){
        this.$confirm('此操作将删除该任务, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            this.deleteRequest("/api/admin/task/delete/"+id).then(resp=>{
              if(resp.code == 200){ //删除成功
                //重新获取用户列表
                this.getTaskList();
              }
            })
        }).catch(() => {
            this.$message({
                type: 'info',
                message: '已取消操作'
            });          
        });
    },
    //编辑任务
    editTask(id){
      this.$store.commit("initTaskId",id);
      this.$router.replace("/admin/task/create");
    }
  }
}
</script>

<style lang="scss" scoped>
.task-container{
  font-family: "华文楷体";
  .box-card{
    border-radius: 10px;
    color:#1A1A1A;
    .search{
      margin-top: 10px;
      display: flex;
      align-items: center;
      color:#1A1A1A;
      font-family: "黑体";
      .searchInput{
        margin-left: 10px;
        border:1px solid #1980FF;
        border-radius: 4px;
      }
      .searchBtn{
        margin: 0 0 0 20px;
      }
    }
    .table{
         margin-top: 10px;
         width: 100%;
         //内容居中
         /deep/.el-table th > .cell {
              text-align: center;
          }
          
          /deep/.el-table .cell {
              text-align: center;
          } 
          .btns{
            display: flex;
            justify-content: space-around;
          }
          .btn{
            border: none;
            font-size: 12px;
            margin:0 10px;
            padding: 0;
            cursor: pointer;
            &:nth-child(1){
              color:#1980FF;
            }
             &:nth-child(2){
              color:#F56C6C;
            }
          }
      }
      .pagination{
        margin: 20px 0 0 0;
        display: flex;
        justify-content: flex-end;
      }
  }
}
</style>